package cc.md888.app.utils;

import cc.md888.app.exception.BizRuntimeException;
import cc.md888.result.ResultCode;
import com.alibaba.fastjson.JSON;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

/**
 * fb工具类
 */
public final class FacebookUtils {



    /**
     * 解析配置文件
     * @param is
     * @return
     */
    public static Map<String, Object> parseLightSharedPreferences(InputStream is){
        try {
            DataInputStream dataStream = new DataInputStream(new BufferedInputStream(is, 512));
            int version = dataStream.readUnsignedByte();
            int size = dataStream.readInt();
            Map<String, Object> values = new HashMap<>();
            for(int index = 0; index < size; index++){
                int type = dataStream.readUnsignedByte();
                String key = dataStream.readUTF();
                switch (type){
                    case 6:
                        int listLen = dataStream.readInt();
                        List<String> listValue = new ArrayList<>();
                        for(int i = 0; i < listLen; i++){
                            String value = dataStream.readUTF();
                            listValue.add(value);
                        }
                        values.put(key, listValue);
                        break;
                    case 5:
                        values.put(key, dataStream.readUTF());
                        break;
                    case 4:
                        values.put(key, dataStream.readDouble());
                        break;
                    case 3:
                        values.put(key, dataStream.readFloat());
                        break;
                    case 2:
                        values.put(key, dataStream.readLong());
                        break;
                    case 1:
                        values.put(key, dataStream.readInt());
                        break;
                    case 0:
                        values.put(key, dataStream.readBoolean());
                        break;
                    default:
                        throw BizRuntimeException.from(ResultCode.ERROR, "不支持的类型:" + type);
                }
            }
            return values;
        } catch (IOException e) {
            throw BizRuntimeException.from(ResultCode.ERROR, "解析FB配置文件失败： " + e.getMessage());
        }
    }



}
